<!-- extend from base layout -->
{% extends "layout.html" %}

{% block extra_css %}

{% endblock %}

{% block content %}
    <div class="container-fluid">
    <ol class="breadcrumb">
        <li><a href="/">后台管理</a></li>
        <li><a href="javascript:void(0);" onclick="$('#menu-order').trigger('click');">订单管理</a></li>
        <li class="active">订单列表</li>
    </ol>

    <form class="form-inline">
        <div class="form-group{% if form.order_id.errors %} has-error{% endif %}">
            {{ form.order_id.label(class="sr-only") }}
            {{ form.order_id(class="form-control", placeholder="订单ID") }}
            {% for error in form.order_id.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.apply_put_id.errors %} has-error{% endif %}">
            {{ form.apply_put_id.label(class="sr-only") }}
            {{ form.apply_put_id(class="form-control", placeholder="申请投资ID") }}
            {% for error in form.apply_put_id.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.apply_get_id.errors %} has-error{% endif %}">
            {{ form.apply_get_id.label(class="sr-only") }}
            {{ form.apply_get_id(class="form-control", placeholder="申请提现ID") }}
            {% for error in form.apply_get_id.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.apply_put_uid.errors %} has-error{% endif %}">
            {{ form.apply_put_uid.label(class="sr-only") }}
            {{ form.apply_put_uid(class="form-control", placeholder="申请投资UID") }}
            {% for error in form.apply_put_uid.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.apply_get_uid.errors %} has-error{% endif %}">
            {{ form.apply_get_uid.label(class="sr-only") }}
            {{ form.apply_get_uid(class="form-control", placeholder="申请提现UID") }}
            {% for error in form.apply_get_uid.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.status_audit.errors %} has-error{% endif %}">
            {{ form.status_audit.label(class="sr-only") }}
            {{ form.status_audit(class="form-control", placeholder="审核状态", data_width="fit", data_header="选择状态") }}
            {% for error in form.status_audit.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.status_pay.errors %} has-error{% endif %}">
            {{ form.status_pay.label(class="sr-only") }}
            {{ form.status_pay(class="form-control", placeholder="支付状态", data_width="fit", data_header="选择状态") }}
            {% for error in form.status_pay.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.status_rec.errors %} has-error{% endif %}">
            {{ form.status_rec.label(class="sr-only") }}
            {{ form.status_rec(class="form-control", placeholder="收款状态", data_width="fit", data_header="选择状态") }}
            {% for error in form.status_rec.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.start_time.errors %} has-error{% endif %}">
            {{ form.start_time.label(class="sr-only") }}
            {{ form.start_time(class="form-control", placeholder="start_time", type='date') }}
            {% for error in form.start_time.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>
        <div class="form-group{% if form.end_time.errors %} has-error{% endif %}">
            {{ form.end_time.label(class="sr-only") }}
            {{ form.end_time(class="form-control", placeholder="end_time", type='date') }}
            {% for error in form.end_time.errors %}
                <span class="help-block">{{ error }}</span>
            {% endfor %}
        </div>

        <button type="submit" class="btn btn-primary" name="op" value="0">Search</button>
        <button type="submit" class="btn btn-primary" name="op" value="1">Export</button>
    </form>

    <hr/>

    <div class="table-responsive">
        {# <table class="table table-striped"> #}
        <table class="table table-hover">
            <thead>
            <tr>
                <th>订单ID</th>
                <th>投资ID</th>
                <th>提现ID</th>
                <th>投资用户</th>
                <th>提现用户</th>
                <th>订单金额</th>
                <th>认证状态</th>
                <th>支付状态</th>
                <th>确认状态</th>
                <th>匹配时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for order, user_profile_put, user_profile_get in pagination.items %}
            <tr class="text-muted">
                <td>{{ order.id }}</td>
                <td>{{ order.apply_put_id }}</td>
                <td>{{ order.apply_get_id }}</td>
                <td>{{ user_profile_put.nickname }} [{{ order.apply_put_uid }}]</td>
                <td>{{ user_profile_get.nickname }} [{{ order.apply_get_uid }}]</td>
                <td>{{ order.money }}</td>
                <td>{{ order.status_audit | status_audit }}</td>
                <td>{{ order.status_pay | status_pay }}</td>
                <td>{{ order.status_rec | status_rec }}</td>
                <td>{{ moment(order.create_time).format('YYYY-MM-DD HH:mm:ss') }}</td>
                <td>
                    <a href="javascript:void(0);" onclick="order_audit({{ order.id }});" rel="tooltip" title="审核"><span class="glyphicon glyphicon-check"></span></a>
                    <a href="javascript:void(0);" onclick="order_delete({{ order.id }});" rel="tooltip" title="删除"><span class="glyphicon glyphicon-trash"></span></a>
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
        {# 翻页 #}
        {% from "macros.html" import render_pagination %}
        {{ render_pagination(pagination, 'order.lists') }}
    </div>
    </div>
{% endblock %}

{% block extra_js %}
<script>
    // 初始化赋值
    var status_audit = $('#status_audit');
    status_audit.selectpicker('val', '{{ form.status_audit.data }}');
    // 处理选项修改
    status_audit.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_audit').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    var status_pay = $('#status_pay');
    status_pay.selectpicker('val', '{{ form.status_pay.data }}');
    // 处理选项修改
    status_pay.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_pay').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    var status_rec = $('#status_rec');
    status_rec.selectpicker('val', '{{ form.status_rec.data }}');
    // 处理选项修改
    status_rec.on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
        // console.log($('#status_rec').val());
        // console.log(event);
        // console.log(clickedIndex);
        // console.log(newValue);
        // console.log(oldValue);
    });

    // 订单审核
    function order_audit(order_id){
        if(confirm("是否确认审核通过?"))
        {
            // console.log(order_id);
            $.getJSON('{{ url_for('order.ajax_audit') }}',
            {
                order_id: order_id
            }, function (result) {
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                    window.location.reload();
                }
            });
            return false;
        }
    }

    // 订单删除
    function order_delete(order_id){
        if(confirm("删除数据不能恢复，是否确认删除?"))
        {
            // console.log(order_id);
            $.getJSON('{{ url_for('order.ajax_delete') }}',
            {
                order_id: order_id
            }, function (result) {
                if(result.hasOwnProperty('error')){
                    alert(result.error);
                }else {
                    alert(result.success);
                    window.location.reload();
                }
            });
            return false;
        }
    }
</script>
{% endblock %}
